package com.china.produce.config.schedule;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/3/9
 */
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(ScheduleConfig.class);

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("ScheduleConfig-%d")
                .setUncaughtExceptionHandler((t, e) -> {
                    logger.error("func=configureTasks t=" + t.getName(), e);
                })
                .build();


        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5, threadFactory));
    }

}
